<?php

namespace App\Models;

use Session;
use Cookie;

class Securitycodeout extends BaseModel
{
    protected $table = "security_code_out";

    const PREFIX = 'ag_';


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    /**
     *插入数据
     */
    public function addAll($data)
    {
        return $this->insert($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 获取防伪码列表
     * @return mixed
     */
    public function getSecurityList(){
        $where['bid'] = session('adminId');
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 10);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'],request('page',1),10);
        }
        return $rows;
    }

    public function checkCode($request)
    {
        $where['code'] = $request['code'];
        $res = $this->getOne($where);
        if (!$res) {
            return '该防伪码不存在';
        }
        return '恭喜您，该防伪码是真的！';
    }

}